package org.dreamwork.jasmine2.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-23
 * Time: 14:23:22
 */
public class StringConverter {
    public static String contvertToString (Object value) {
        if (value == null) return "";
        if (value.getClass () == String.class) return (String) value;
        return value.toString ();
    }

    public static String asString (Object value, boolean allowNull) {
        if (value == null && allowNull) return null;
        return contvertToString (value);
    }

    public static Object convertFromString (Class type, String expression) {
        if (expression == null) return null;
        expression = expression.trim ();
        if (type == String.class || type == Object.class)
            return expression;
        if (type == Integer.class || type == int.class)
            return Integer.parseInt (expression.trim ());
        if (type == Double.class || type == double.class)
            return Double.parseDouble (expression.trim ());
        if (type == Long.class || type == long.class)
            return Long.parseLong (expression.trim ());
        if (type == Short.class || type == short.class)
            return Short.parseShort (expression);
        if (type == Character.class || type == char.class)
            return (char)(expression.charAt (0) - '0');
        if (type == Byte.class || type == byte.class)
            return Byte.parseByte (expression);
        if (type == Boolean.class || type == boolean.class)
            return Boolean.valueOf (expression);
        if (Date.class.isAssignableFrom (type)) {
            try {
                return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss").parse (expression);
            } catch (Exception ex) {
                try {
                    return new SimpleDateFormat ("yyyy-MM-dd").parse (expression);
                } catch (Exception e) {
                    throw new ClassCastException ("can't cast '" + expression + "' to java.util.Date");
                }
            }
        }
        if (type == BigDecimal.class) return new BigDecimal (expression);
        if (type == BigInteger.class) return new BigInteger (expression);

        throw new ClassCastException ("can't cast '" + expression + "' to " + type.getName ());
    }

    public static boolean stringEquals (Object value1, Object value2) {
        return stringEquals (value1, value2, false);
    }

    public static boolean stringEquals (Object value1, Object value2, boolean ignoreCase) {
        if (value1 == null || value2 == null) return false;
        if (ignoreCase)
            return value1.toString ().equalsIgnoreCase (value2.toString ());
        return value1.toString ().equals (value2.toString ());
    }

    public static boolean isEmpty (String string) {
        return (string == null || string.trim ().length () == 0);
    }

    public static boolean equals (Object value1, Object value2) {
        if (value1 == value2) return true;
        if (value1 == null || value2 == null) return false;
        if (value1.getClass () == value2.getClass ()) return value1.equals (value2);
        String s1 = value1.toString ();
        String s2 = value2.toString ();
        return s1.equals (s2);
    }

    public static boolean contains (Collection collection, Object value) {
        if (collection.contains (value)) return true;

        for (Object other : collection) {
            if (equals (value, other)) return true;
        }
        
        return false;
    }

    public static String generateUUID () {
        long timestamp = System.currentTimeMillis ();
        long uiid = timestamp ^ (long) (timestamp * Math.random ());
        return String.valueOf (uiid);        
    }

    public static String format (Object value, String format) {
        if (value == null) return "";
        if (isEmpty (format)) return value.toString ();

        if (value instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat (format);
            return sdf.format ((Date) value);
        } else if (value instanceof Number) {
            DecimalFormat df = new DecimalFormat (format);
            return df.format (value);
        } else
            return String.format (format, value);
    }
}